package com.cn.wanxi.service;

import com.cn.wanxi.common.Message;
import com.cn.wanxi.common.StringUtil;
import com.cn.wanxi.dao.UserVerifyDao;
import com.cn.wanxi.entity.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @description:
 * @author: mayafei
 * @time: 2020/3/11 16:41
 */
@Service
public class UserVerifySerice {
    @Autowired
     UserVerifyDao userVerifyDao;

    /**
     * 用户登录
     * @param usersEntity
     * @return
     */
    public Message userLogin(UsersEntity usersEntity,HttpServletRequest request){
        UsersEntity user = userVerifyDao.userLogin(usersEntity);
//        封装返回信息
        Message message;
        if(null == user){
            message = new Message(0, "不存在该用户！", user);
        }else {
            message = new Message(1, "登录成功！", user);
//            取出用户详细信息
            UserInfoEntity userInfoEntity = userVerifyDao.userInfo(user);
//            System.out.println(userInfoEntity);
//            登录用户信息放入session
            HttpSession httpSession = request.getSession();
            httpSession.setAttribute("user",userInfoEntity);
        }
        return message;
    }

    /**
     * 取出用户数据
     * @return
     */
    public Message userInfo(HttpServletRequest request){
        //取出详细信息
        HttpSession httpSession = request.getSession();
        UserInfoEntity user = (UserInfoEntity) httpSession.getAttribute("user");
//        存入用户名和权限
        UsersEntity usersEntity = new UsersEntity();
        usersEntity.setLimits(user.getLimits());
        usersEntity.setUsername(user.getUsername());
//        System.out.println(user.getUsername());
//        返回数据
        Message message = new Message(1,"用户数据",usersEntity);
        return message;
    }

    /**
     * 取出下拉列表
     * @param selectOptionEntity
     * @return
     */
    public Message selectOption(SelectOptionEntity selectOptionEntity){
        List<SelectOptionEntity> selectOptionEntities = userVerifyDao.selectOption(selectOptionEntity);
        Message message;
        if(0 == selectOptionEntities.size()){
            message = new Message(0,"没有该类型下拉列表",selectOptionEntities);
        }else {
            message = new Message(1,"查询成功",selectOptionEntities);
        }
        return message;
    }

    /**
     * 提出数据
     * @param pagingEntity
     * @return
     */
    public Message dataInfo(PagingEntity pagingEntity){
//        查询出当页展示数据
        Map<Object,Object> map = new HashMap<>();
        List<UserInfoEntity> userInfoEntities = userVerifyDao.dataInfo(pagingEntity);
        map.put("data",userInfoEntities);
//        查询出总数居条数
        pagingEntity.setStart(-1);
        List<UserInfoEntity> userInfoEntities1 = userVerifyDao.dataInfo(pagingEntity);
        map.put("sum",userInfoEntities1.size());
//        封装到message
        Message message = new Message(1,"查询成功",map);
        return message;
    }

    /**
     * 取出导航数据
     * @param type
     * @return
     */
    public Message navigationData(String type){
        List<NavigationEntity> navigationEntities = userVerifyDao.navigationData(type);
        Message message = new Message(1,"导航数据",navigationEntities);
        return message;
    }

    /**
     * 删除用户信息
     * @return
     */
    public Message deleteUser(DeleteUser deleteUser){
        int i = userVerifyDao.deleteUser(deleteUser);
        Message message = null;
        if(i>0){
            message = new Message(1,"删除成功",i);
        }else {
            message = new Message(0,"删除失败，用户数据异常",i);
        }
        return message;
    }

    /**
     * 删除多条数据
     * @param allID
     * @param role
     * @return
     */
    public Message moreDelete(String allID,String role){
        Map<Object,Object> map = new HashMap<>();
        map.put("role",role);
        //将id封装到list
        int[] ints = StringUtil.stringToArray(allID, ",");
        List<DeleteUser> list = new ArrayList<>();
        for (int id:ints) {
            DeleteUser information = new DeleteUser();
            information.setId(id);
            list.add(information);
        }
        map.put("list",list);
        int i = userVerifyDao.moreDelete(map);
        Message message = new Message(1,"成功删除"+(i/2)+"条数据！",i);
        return message;
    }

}
